import { Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { ApplicantService } from './applicant.service';
import { MeterService } from './meter.service';
import { DepartmentService } from './department.service';
import { AddrService } from './addr.service';
import { setState } from '../store/utils/utils.actions';
import { cloneDeep } from 'lodash';
import {
    getArea,
    getCity,
    getDistrict,
    getTown,
    getVillage,
} from '../store/utils/utils.reducer';
@Injectable()
export class UtilsService {
    city = [];
    district = [];
    town = [];
    village = [];
    area = [];
    constructor(
        private store: Store,
        private applicantService: ApplicantService,
        private meterService: MeterService,
        private departmentService: DepartmentService,
        private addrService: AddrService
    ) {
        this.store.pipe(select(getCity)).subscribe((data) => {
            this.city = data ? cloneDeep(data) : [];
        });
        this.store.pipe(select(getDistrict)).subscribe((data) => {
            this.district = data ? cloneDeep(data) : [];
        });
        this.store.pipe(select(getTown)).subscribe((data) => {
            this.town = data ? cloneDeep(data) : [];
        });
        this.store.pipe(select(getVillage)).subscribe((data) => {
            this.village = data ? cloneDeep(data) : [];
        });
        this.store.pipe(select(getArea)).subscribe((data) => {
            this.area = data ? cloneDeep(data) : [];
        });
    }
    async init() {
        const applicant =
            (await this.applicantService.fetchApplicantByName({
                applicant_name: null,
            })) || [];
        //console.log(applicant)
        const meterType = (await this.meterService.getMeterTypes()) || [];
        //console.log(meterType)
        const department =
            (await this.departmentService.fetchDepartmentsByNone()) || [];
        //console.log(department);
        const city = (await this.addrService.fetchCityInfo()) || [];
        //console.log('city', city);
        const district = (await this.addrService.fetchDistrictInfo()) || [];
        //console.log('district', district);
        const town = (await this.addrService.fetchTownInfo()) || [];
        ///console.log('town', town);
        const village = (await this.addrService.fetchVillageInfo()) || [];
        //console.log('village', village);
        const area = (await this.addrService.fetchAreaInfo()) || [];
        //console.log('area', area);
        const menuCollapse = [];
        const t = null;
        const update = null;
        this.store.dispatch(
            setState({
                utils: {
                    applicant,
                    meterType,
                    department,
                    city,
                    district,
                    town,
                    village,
                    area,
                    menuCollapse,
                    t,
                    update,
                },
            })
        );
    }
    getUtil() {
        this.store.pipe(select(getCity)).subscribe((data) => {
            this.city = data ? cloneDeep(data) : [];
        });
        this.store.pipe(select(getDistrict)).subscribe((data) => {
            this.district = data ? cloneDeep(data) : [];
        });
        this.store.pipe(select(getTown)).subscribe((data) => {
            this.town = data ? cloneDeep(data) : [];
        });
        this.store.pipe(select(getVillage)).subscribe((data) => {
            this.village = data ? cloneDeep(data) : [];
        });
        this.store.pipe(select(getArea)).subscribe((data) => {
            this.area = data ? cloneDeep(data) : [];
        });
    }
    addressOption() {
        this.getUtil();
        this.village.forEach((item) => {
            const idx = this.town.findIndex((it) => item.pid === it.id);
            if (idx >= 0) {
                this.town[idx].children = (
                    this.town[idx].children || []
                ).concat(item);
            }
        });
        this.town.forEach((item) => {
            const idx = this.district.findIndex((it) => item.pid === it.id);
            if (idx >= 0 && item.children && item.children.length > 0) {
                this.district[idx].children = (
                    this.district[idx].children || []
                ).concat(item);
            }
        });
        this.district.forEach((item) => {
            const id = item.id;
            item.id = {
                id,
                isArea: false,
            };
            const idx = this.city.findIndex((it) => item.pid === it.zoneId);
            if (idx >= 0 && item.children && item.children.length > 0) {
                this.city[idx].children = (
                    this.city[idx].children || []
                ).concat(item);
            }
        });
        this.area.forEach((item) => {
            const id = item.id;
            item.id = {
                id,
                isArea: true,
            };
            const idx = this.city.findIndex((it) => item.pid === it.zoneId);
            if (idx >= 0) {
                this.city[idx].children = (
                    this.city[idx].children || []
                ).concat(item);
            }
        });
        if (this.city) {
            const res = this.city.map((item) => {
                return {
                    ...item,
                    name: item.city,
                    id: item.zoneId,
                };
            });
            return res;
        } else {
            return [];
        }
    }
    districtOptions() {
        this.getUtil();
        this.district.forEach((item) => {
            const id = item.id;
            const idx = this.city.findIndex((it) => item.pid === it.zoneId);
            console.log('aaaa', this.city[idx]);
            if (idx >= 0) {
                this.city[idx].children = (
                    this.city[idx].children || []
                ).concat(item);
            }
        });
        if (this.city) {
            const res = this.city.map((item) => {
                return {
                    ...item,
                    name: item.city,
                    id: item.zoneId,
                };
            });
            return res;
        } else {
            return [];
        }
    }
    townOptions() {
        this.getUtil();
        this.town.forEach((item) => {
            const idx = this.district.findIndex((it) => item.pid === it.id);
            if (idx >= 0) {
                this.district[idx].children = (
                    this.district[idx].children || []
                ).concat(item);
            }
        });
        this.district.forEach((item) => {
            const id = item.id;
            const idx = this.city.findIndex((it) => item.pid === it.zoneId);
            if (idx >= 0 && item.children && item.children.length > 0) {
                this.city[idx].children = (
                    this.city[idx].children || []
                ).concat(item);
            }
        });
        if (this.city) {
            const res = this.city.map((item) => {
                return {
                    ...item,
                    name: item.city,
                    id: item.zoneId,
                };
            });
            return res;
        } else {
            return [];
        }
    }
    cityOptions() {
        this.getUtil();
        if (this.city) {
            const res = this.city.map((item) => {
                return {
                    ...item,
                    name: item.city,
                    id: item.zoneId,
                };
            });
            return res;
        } else {
            return [];
        }
    }
}
